//
//  PhotoAlbumViewController.m
//  NBTripe
//
//  Created by Lee on 16/1/14.
//  Copyright © 2016年 Vicki. All rights reserved.
//

#import "PhotoAlbumViewController.h"
#import "CustomDetailTextModel.h"
#import "CustomDetailTextTableViewCell.h"
#import "PhotoCollectionViewCell.h"
#import "CustomDaysWayPointsModel.h"
@interface PhotoAlbumViewController ()<UICollectionViewDelegate, UICollectionViewDataSource>

@property (nonatomic, strong) UICollectionView *collectionView;
//自动播放定时器
@property (nonatomic, strong) NSTimer *timer;

@end

@implementation PhotoAlbumViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self createCollectionView];
    [self.collectionView setContentOffset:CGPointMake(_row*WIDTH, 0) animated:YES];
    self.navigationController.navigationBar.translucent = YES;
    self.view.backgroundColor = [UIColor blackColor];

    
    // Do any additional setup after loading the view.
}

- (void)createCollectionView {
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    flowLayout.itemSize = CGSizeMake(WIDTH, HIGHT);
    flowLayout.minimumLineSpacing = 0;
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HIGHT) collectionViewLayout:flowLayout];
    //scrollview关闭适配
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.collectionView.dataSource = self;
    self.collectionView.delegate = self;
    self.collectionView.bounces = NO;
    self.collectionView.backgroundColor = [UIColor colorWithRed:0.96 green:0.89 blue:0.7 alpha:1];
    [self.collectionView registerClass:[PhotoCollectionViewCell class] forCellWithReuseIdentifier:@"PhotoCollectionViewCell"];
    self.collectionView.pagingEnabled = YES;
    [self.view addSubview:self.collectionView];
}
/**
 *  collectionView协议方法
 */
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.informationArray.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
  PhotoCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"PhotoCollectionViewCell" forIndexPath:indexPath];
    //第几天
    NSInteger days = [self.informationArray[indexPath.row] day];
    cell.titleLabel.text = [NSString stringWithFormat:@"第%ld天 %ld/%ld", days, indexPath.row+1, self.informationArray.count];
    cell.photomodel = self.informationArray[indexPath.row];
    cell.pic_imageviewscroview.zoomScale = 1;
    //cell.pic_imageviewscroview.center = self.view.center;
    void (^backBlcok)() = ^(){
        [self dismissViewControllerAnimated:YES completion:^{
            
        }];
    };
    void (^timerBlock)() = ^(){
        self.timer = [NSTimer scheduledTimerWithTimeInterval:3 target:self selector:@selector(timeChange) userInfo:nil repeats:YES];
        [self.timer setFireDate:[NSDate distantPast]];
    };
    void (^pauseBlock)() = ^(){
        [self.timer setFireDate:[NSDate distantFuture]];
    };
    cell.block = backBlcok;
    //如果已经到了最后一页就不能自动播放
    cell.playblock = timerBlock;
    //暂停播放
    cell.pauseblock = pauseBlock;
    return cell;
}
/**
 *  定时器自动播放
 */
- (void)timeChange {
   [self.collectionView setContentOffset:CGPointMake(_collectionView.contentOffset.x+WIDTH, 0) animated:YES];
    //当播放到最后一页的时候停止定时器
    if ( _collectionView.contentOffset.x == (self.informationArray.count-2)*WIDTH) {
   [self.timer setFireDate:[NSDate distantFuture]];
    }
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
